//
//  SetPasswordView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2024/12/26.
//

import SwiftUI

struct AiChatView: View {
    var forceShow = false
    @EnvironmentObject var themeManager: ThemeManager
    @EnvironmentObject var appStorageManager: AppStorageManager
    
    @State private var isShow = false
    @State private var aiModeStr = ""
    
    var body: some View {
        HStack{
            VStack(alignment: .leading, spacing: 18) {
                HStack{
                    Text("聊天")
                        .foregroundColor(.bgItemContent)
                        .fontWeight(.bold)
                        .font(.system(size: 14))
                    Spacer()
                    Image(.myIconExpand)
                        .resizable()
                        .frame(width: 15,height: 15)
                        .rotationEffect(isShow ? Angle(degrees: 180) : .zero)
                }.contentShape(Rectangle())// 将整个矩形区域（包括padding）都设置为可点击区域
                    .onTapGesture {
                        withAnimation(Animation.easeInOut(duration: 0.3)) {
                            if !forceShow {
                                isShow.toggle()
                            }
                        }
                    }
                
                if isShow {
                    HStack {
                        Text("聊天模型")
                            .foregroundColor(.bgItemContent)
                            .font(.system(size: 14))
                        Spacer()
                        Text(aiModeStr)
                            .foregroundColor(.bgNormalText4)
                            .font(.system(size: 10))
                        Image(.appIconArrowRight)
                            .resizable()
                            .frame(width: 15,height: 15)
                        
                    }.padding(.top,5).padding(.bottom,5)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            showFunctionPopup()
                        }
                    HStack {
                        VStack(alignment: .leading, spacing: 4) {
                            Text("互动模式")
                                .font(.system(size: 14))
                                .foregroundColor(.bgItemContent)
                            
                            Text("多条回复、表情包、语音、拍一拍、引用等相关功能")
                                .font(.system(size: 10))
                                .foregroundColor(.bgItemContent2)
                        }.frame(maxWidth: .infinity, alignment: .leading)  // 关键：限制VStack宽度
                            .layoutPriority(1)  // 提高VStack的布局优先级
                        Spacer()
                        Toggle("", isOn: appStorageManager.$toggleChatFunc)
                            .toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
                    }
//                    HStack {
//                        VStack(alignment: .leading, spacing: 4) {
//                            Text("简短聊天模式")
//                                .font(.system(size: 14))
//                                .foregroundColor(.bgItemContent)
//                            
//                            Text("切换开关时建议结束一下话题，避免上下文干扰")
//                                .font(.system(size: 10))
//                                .foregroundColor(.bgItemContent2)
//                        }.frame(maxWidth: .infinity, alignment: .leading)  // 关键：限制VStack宽度
//                            .layoutPriority(1)  // 提高VStack的布局优先级
//                        
//                        
//                        Spacer()
//                        Toggle("", isOn: appStorageManager.$toggleAiShort)
//                            .toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
//                    }
//                    HStack {
//                        Toggle("表情动作描述", isOn: appStorageManager.$toggleAiDesc)
//                            .foregroundColor(.bgItemContent)
//                            .toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
//                            .font(.system(size: 14))
//                        
//                    }.padding(.top,5).padding(.bottom,5)
//                    HStack {
//                        VStack(alignment: .leading, spacing: 4) {
//                            Text("表情包")
//                                .font(.system(size: 14))
//                                .foregroundColor(.bgItemContent)
//                            
//                            Text("需要先开启 '简短聊天模式'")
//                                .font(.system(size: 10))
//                                .foregroundColor(.bgItemContent2)
//                        }.frame(maxWidth: .infinity, alignment: .leading)
//                            .layoutPriority(1)  // 提高VStack的布局优先级
//                        Spacer()
//                        Toggle("", isOn: appStorageManager.$toggleEmojiPicture)
//                            .toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
//                    }
                }
            }
            Spacer()
        }.frame(maxWidth: .infinity).padding(18).background(.bgItem).clipShape(RoundedRectangle(cornerRadius: 15))
            .onChange(of: appStorageManager.aiModel) { value in
                getChatModelStr()
            }
            .onChange(of: appStorageManager.toggleAiShort) { value in
                if !value {
                    //微信聊天模式关闭，也需要关闭 表情包模式
                    appStorageManager.toggleEmojiPicture = false
                }
            }
            .onChange(of: appStorageManager.toggleEmojiPicture) { value in
                if value && !appStorageManager.toggleAiShort {
                    //如果微信模式关闭，则不允许打开
                    appStorageManager.toggleEmojiPicture = false
                    showTipsDialog(text: "请先打开 ‘简短聊天模式’")
                }
            }
            .onAppear{
                if forceShow {
                    isShow = true
                }
                getChatModelStr()
            }
    }
    
    func getChatModelStr() {
        if AiCustomUtils.shared.isCustomAi(type: IAiModelType.chat.rawValue) {
            aiModeStr = "自定义模型"
        } else {
            switch AppStorageManager.shared.aiModel {
            case .TYPE_0:
                aiModeStr = "聊天模型1"
            case .TYPE_1:
                aiModeStr = "聊天模型2"
            }
        }
    }
    
    //聊天模型弹窗
    func showFunctionPopup() {
        if AiCustomUtils.shared.isCustomAi(type: IAiModelType.chat.rawValue) {
            showToast("当前为自定义模型，无法手动切换模型")
            return
        }
        SwiftEntryKitWrapper.showBottomPopup { dismiss in
            ChatModelDialogView() {
                dismiss()
            }
        }
    }
}

//#Preview {
//    SetPasswordView()
//}

//定义预览提供者
struct AiChat_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: AiChatView())
    }
}
